/**
 * 
 */
package com.sahyadri.web.util.service.adpater;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.google.gdata.data.calendar.CalendarEventEntry;
import com.sahyadri.web.bean.Category;
import com.sahyadri.web.bean.CategoryEntry;
import com.sahyadri.web.bean.Events;
import com.sahyadri.web.bean.GalleryDetails;
import com.sahyadri.web.bean.Location;
import com.sahyadri.web.bean.Menu;
import com.sahyadri.web.bean.MenuEntry;
import com.sahyadri.web.bean.MenuSearchCriteria;
import com.sahyadri.web.bean.Offer;
import com.sahyadri.web.bean.Organization;
import com.sahyadri.web.bean.Review;
import com.sahyadri.web.bean.SearchResults;
import com.sahyadri.web.google.calendar.CalendarClientService;

/**
 * Concrete implementation of AuthenticationServiceAdapter
 * 
 * 
 */
public class OrganizationServiceAdapterImpl implements OrganizationServiceAdapter {
	
	private static final Logger log = Logger
			.getLogger(OrganizationServiceAdapterImpl.class);
	
	@Autowired
	private LocationService locationService;
	
    
	/**
	 * @param webServiceTemplate
	 */
	public OrganizationServiceAdapterImpl() {
		super();
	}


	@Override
	public List<Location> getAllLocations(Organization org) {
		List<Location> locations = new ArrayList<Location>();
		try {
			locations = locationService.getLocationsByOrg(org);
			
		}catch (Exception e){
			log.error("error in getAllLocations ", e);
			
		}
		return locations;
	}


	@Override
	public Location getLocation(long locationId) {
		Location location = null;
		try {
			location = locationService.getLocation(locationId);
		}catch (Exception e){
			e.printStackTrace();
		}
		return location;
	}


	@Override
	public Location saveLocation(Organization org, Location location) {
		if(location.getId()==0) {
			try {
				location = locationService.createLocation(org, location);
			}catch(Exception e){
				log.error("error in createLocation() ", e); 
			}
		}
		else{
			try {
				locationService.updateLocation(org, location);
			}catch(Exception e){
				log.error("error in updateLocation() ", e); 
			}
		}
		
		return location;
	}


	@Override
	public void saveMenu(long locationId, Menu menu) {
		if(menu.getId()==0) {
			try {
				menu = locationService.createMenu(locationId, menu);
			}catch(Exception e){
				log.error("error in createMenu() ", e); 
			}
		}
		else{
			try {
				locationService.updateMenu(locationId, menu);
			}catch(Exception e){
				log.error("error in updateMenu() ", e); 
			}
		}
		
		
	}


	@Override
	public List<Menu> getMenuList(long locationId) { 
		List<Menu> menuList = new ArrayList<Menu>();
		try {
			menuList = locationService.getMenuList(locationId);
		}catch (Exception e) {
			log.error("Error in getMenuList() ", e);
		}
		return menuList;
	}


	@Override
	public Menu getMenu(long menuId) {
		Menu menu = null;
		try {
			menu = locationService.getMenu(menuId);
		}catch (Exception e){
			e.printStackTrace();
		}
		return menu;
	}


	@Override
	public Category getCategory(long categoryId) {
		Category category = null;
		try {
			category = locationService.getCategory(categoryId);
		}catch (Exception e){
			log.error("Error in getCatgory() ", e);
		}
		return category;
	}


	@Override
	public void saveCategory(long locationId, Category category) {
		if(category.getId()==0) {
			try {
				category = locationService.createCategory(locationId, category);
			}catch(Exception e){
				log.error("error in createCategory() ", e); 
			}
		}
		else{
			try {
				locationService.updateCategory(locationId, category);
			}catch(Exception e){
				log.error("error in updateCategory() ", e); 
			}
		}
		
	}


	@Override
	public List<Category> getCategoryList(long locationId) {
		List<Category> categoryList = new ArrayList<Category>();
		try {
			categoryList = locationService.getCategoryList(locationId);
		}catch (Exception e) {
			log.error("Error in getCategoryList() ", e);
		}
		return categoryList;
	}


	@Override
	public void saveReview(long locationId, Review review) {
		if(review.getId()==0) {
			try {
				review = locationService.createReview(locationId, review);
			}catch(Exception e){
				log.error("error in createReview() ", e); 
			}
		}
		else{
			try {
				locationService.updateReview(locationId, review);
			}catch(Exception e){
				log.error("error in updateReview() ", e); 
			}
		}
		
	}


	@Override
	public List<Review> getReviewList(long locationId) {
		List<Review> reviewList = new ArrayList<Review>();
		try {
			reviewList = locationService.getReviewList(locationId);
		}catch (Exception e) {
			log.error("Error in getReviewList() ", e);
		}
		return reviewList;
	}


	@Override
	public Review getReview(long reviewId) {
		Review review = null;
		try {
			review = locationService.getReview(reviewId);
		}catch (Exception e){
			log.error("Error in getReview() ", e);
		}
		return review;
	}


	@Override
	public SearchResults<MenuEntry> searchMenus(MenuSearchCriteria criteria) {
		SearchResults<MenuEntry> results = new SearchResults<MenuEntry>();
		List<Menu> menuList = new ArrayList<Menu>();
		try {
			results = locationService.searchMenuList(criteria);
		}catch (Exception e) {
			log.error("Error in getMenuList() ", e);
		}
		
		return results;
	}


	@Override
	public List<Events> getEventList(long locationId) {
		List<Events> eventList = new ArrayList<Events>();
		try {
			eventList = locationService.getEventList(locationId);
		}catch (Exception e) {
			log.error("Error in getEventList() ", e);
		}
		return eventList;
	}


	@Override
	public Object getEvent(long eventId) {
		Events event = null;
		try {
			event = locationService.getEvent(eventId);
		}catch (Exception e){
			log.error("Error in getEvent() ", e);
		}
		return event;
	}


	@Override
	public void saveEvent(long locationId, Events event, String action) {
		if(event.getId()==0) {
			try {
				event = locationService.createEvent(locationId, event);
				try {
					CalendarClientService calendarClient = new CalendarClientService("sahyadrirestaurant@gmail.com", "sahyadri28122012");
					CalendarEventEntry eventEntry  = calendarClient.createEntryObject(event);
					calendarClient.insertEntry(eventEntry);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					log.error("Error in Creating Event", e);
				}
			}catch(Exception e){
				log.error("error in createEvent() ", e); 
			}
		}
		else{
			try {
				locationService.updateEvent(locationId, event);
				try {
					CalendarClientService calendarClient = new CalendarClientService("sahyadrirestaurant@gmail.com", "sahyadri28122012");
					CalendarEventEntry eventEntry  = calendarClient.sendFullTextQuery(event);
					eventEntry = calendarClient.updateEntryObject(eventEntry, event);
					calendarClient.updateEvent(eventEntry);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					log.error("Error in Creating Event", e);
				}
			}catch(Exception e){
				log.error("error in updateEvent() ", e); 
			}
		}
		
		
	}


	@Override
	public List<Offer> getOfferList(long locationId) {
		List<Offer> offerList = new ArrayList<Offer>();
		try {
			offerList = locationService.getOfferList(locationId);
		}catch (Exception e) {
			log.error("Error in getOfferList() ", e);
		}
		return offerList;
	}


	@Override
	public Object getOffer(long offerId) {
		Offer offer = null;
		try {
			offer = locationService.getOffer(offerId);
		}catch (Exception e){
			log.error("Error in getOffer() ", e);
		}
		return offer;
	}


	@Override
	public void saveOffer(long locationId, Offer offer) {
		if(offer.getId()==0) {
			try {
				offer = locationService.createOffer(locationId, offer);
			}catch(Exception e){
				log.error("error in createOffer() ", e); 
			}
		}
		else{
			try {
				locationService.updateOffer(locationId, offer);
			}catch(Exception e){
				log.error("error in updateOffer() ", e); 
			}
		}
		
	}


	@Override
	public Menu assignMenu(long categoryId, long menuId) {
			Menu menu = null;
			try {
				menu = locationService.assignMenu(categoryId, menuId);
			}catch(Exception e){
				log.error("error in assignMenu() ", e); 
			}
			return menu;
	}


	@Override
	public List<MenuEntry> getCategoryMenuList(long categoryId) {
		List<MenuEntry> results = new ArrayList<MenuEntry>();
		Category category = null;
		try {
			category = locationService.getCategory(categoryId);
		}catch(Exception e){
			log.error("error in assignMenu() ", e); 
		}
		if(category != null) {
			for(Menu menu : category.getMenus()) {
				results.add(new MenuEntry(menu));
			}
		}
		return results;
	}


	@Override
	public List<GalleryDetails> getGalleryList(long locationId) {
		List<GalleryDetails> galleryList = new ArrayList<GalleryDetails>();
		try {
			galleryList = locationService.getGalleryList(locationId);
		}catch (Exception e) {
			log.error("Error in getGalleryList() ", e);
		}
		return galleryList;
	}


	@Override
	public GalleryDetails getGallery(long galleryId) {
		GalleryDetails gallery = null;
		try {
			gallery = locationService.getGallery(galleryId);
		}catch (Exception e){
			log.error("Error in getGallery() ", e);
		}
		return gallery;
	}


	@Override
	public void saveGallery(long locationId, GalleryDetails gallery) {
		if(gallery.getId()==0) {
			try {
				gallery = locationService.createGallery(locationId, gallery);
			}catch(Exception e){
				log.error("error in createGallery() ", e); 
			}
		}
		else{
			try {
				locationService.updateGallery(locationId, gallery);
			}catch(Exception e){
				log.error("error in updateGallery() ", e); 
			}
		}
		
	}


	@Override
	public void deleteGallery(long locationId, long galleryId) {
		try {
			locationService.deleteGallery(locationId, galleryId);
		}catch(Exception e){
			log.error("error in createGallery() ", e); 
		}
		
	}


	@Override
	public void deleteEvent(long locationId, long eventId) {
		try {
			locationService.deleteEvent(locationId, eventId);
		}catch(Exception e){
			log.error("error in deleteEvent() ", e); 
		}		
	}


	@Override
	public void deleteOffer(long locationId, long offerId) {
		try {
			locationService.deleteOffer(locationId, offerId);
		}catch(Exception e){
			log.error("error in deleteOffer() ", e); 
		}
		
	}


	@Override
	public void deleteMenu(long locationId, long menuId) {
		try {
			locationService.deleteMenu(locationId, menuId);
		}catch(Exception e){
			log.error("error in deleteMenu() ", e); 
		}
		
	}


	@Override
	public void deleteCategory(long locationId, long categoryId) {
		try {
			locationService.deleteCategory(locationId, categoryId);
		}catch(Exception e){
			log.error("error in deleteCategory() ", e); 
		}
		
	}


	@Override
	public void deleteLocation(Organization org, long locationId) {
		try {
			locationService.deleteLocation(org, locationId);
		}catch(Exception e){
			log.error("error in deleteCategory() ", e); 
		}
		
	}


	@Override
	public void deleteReview(long locationId, long reviewId) {
		try {
			locationService.deleteReview(locationId, reviewId);
		}catch(Exception e){
			log.error("error in deleteReview() ", e); 
		}
		
	}


	@Override
	public List<Location> getAllLocations() {
		List<Location> locations = new ArrayList<Location>();
		try {
			locations = locationService.getAllLocation();
		}catch(Exception e){
			log.error("error in getAllLocations() ", e); 
		}
		return locations;
	}


	@Override
	public List<Review> getPublishedReviews(long locationId) {
		List<Review> reviewList = new ArrayList<Review>();
		try {
			reviewList = locationService.getPublishedReviews(locationId);
		}catch(Exception e){
			log.error("error in getPublishedReviews() ", e); 
		}
		return reviewList;
	}


	@Override
	public List<CategoryEntry> getPublicCategoryList(long locationId) {
		List<Category> categoryList = new ArrayList<Category>();
		try {
			categoryList = locationService.getCategoryList(locationId);
		}catch (Exception e) {
			log.error("Error in getCategoryList() ", e);
		}
		List<CategoryEntry> categoryEntryList = new ArrayList<CategoryEntry>();
		for(Category category: categoryList) {
			categoryEntryList.add(new CategoryEntry(category.getId(), category.getName()));
		}
		return categoryEntryList;
	}


	
	


	

	
	

}
